<template>
  <div>
    <el-dialog
      :title="isadd ? '添加菜单' : '编辑菜单'"
      :visible.sync="isshow"
      :before-close="cancel"
      center
    >
      <el-form ref="menu" :model="menuinfo" :rules="rules">
        <el-form-item
          prop="title"
          label="菜单名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="menuinfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">
          <el-select @change="change" v-model="menuinfo.pid" placeholder="请选择上级菜单">
            <el-option label="上级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menulist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="菜单类型" :label-width="formLabelWidth">
          <el-radio disabled v-model="menuinfo.type" :label="1">目录</el-radio>
          <el-radio disabled v-model="menuinfo.type" :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item
          v-if="menuinfo.type == 1"
          label="菜单图标"
          :label-width="formLabelWidth"
        >
          <el-select v-model="menuinfo.icon" placeholder="请选择">
            <el-option
              v-for="item in iconlist"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
         <el-select v-model="menuinfo.url" placeholder="请选择">
            <el-option
              v-for="item in indexRoutes"
              :key="item.path"
              :label="item.path"
              :value="item.path"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuinfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="isadd" type="primary" @click="addInfo">添加</el-button>
        <el-button @click="editInfo" v-else type="primary">编辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { menuAdd, menuInfo, menuEdit } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
import {indexRouter} from '../../router'
export default {
  data() {
    return {
      menuinfo: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }],
      },
      formLabelWidth: "120px",
        iconlist: [
        "el-icon-s-tools",
        "el-icon-setting",
        "el-icon-s-goods",
        "el-icon-goods",
      ],
      indexRoutes: indexRouter,
    };
  },
  props: ["isshow", "isadd"],
  methods: {
    ...mapActions({
      getMenu: "menu/getMenuAction",
    }),
    change(){
      console.log("11111");
      this.menuinfo.type=this.menuinfo.pid==0?1:2;
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    reset() {
      this.menuinfo = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
      this.$refs.menu.resetFields();
    },
    addInfo() {
      this.$refs.menu.validate((val) => {
        if (val) {
          // console.log(this.menuinfo,'输出表单');
          //调用添加接口
          menuAdd(this.menuinfo).then((res) => {
            // console.log(res, "菜单添加");
            if (res.code == 200) {
              this.$message.success(res.msg);
              //添加成功关闭弹窗
              this.cancel();
              this.getMenu();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("验证失败");
        }
      });
    },
    //查询一条数据方法
    lookup(id) {
      console.log(id, "列表的id");
      menuInfo({ id }).then((res) => {
        // console.log(res);
        if (res.code == 200) {
          //  console.log(this.menuInfo,'1111');
          this.menuinfo = res.list;
          this.menuinfo.id = id; //给编辑表单对象添加id
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //封装一个菜单编辑
    editInfo() {
      this.$refs.menu.validate((val) => {
        if (val) {
          // console.log(this.menuinfo,'输出表单');
          //调用添加接口
          menuEdit(this.menuinfo).then((res) => {
            // console.log(res, "菜单添加");
            if (res.code == 200) {
              this.$message.success(res.msg);
              //添加成功关闭弹窗
              this.cancel();
              this.getMenu();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("验证失败");
        }
      });
    },
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu",
    }),
  },
};
</script>

<style scoped>
</style>
